package com.zhoug.router3demo;

import android.content.Context;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.zhoug.router3.Router;

import java.util.Arrays;
import java.util.List;

/**
 * 登录拦截器
 *
 * @Author: zhoug
 * @Date: 2025-01-21
 * @Description:
 */
@Interceptor(priority = 1)
public class LoginInterceptor implements IInterceptor {

    //需要等了才能访问的页面
    private static final List<String> paths = Arrays.asList(
            Paths.UserHomeActivity
    );

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        String path = postcard.getPath();
        if (paths.contains(path) && !isLogin()) {
            Bundle extras = postcard.getExtras();
            Bundle loginArgs=new Bundle();
            //登录后跳转的页面
            loginArgs.putBundle("targetArgs",extras);
            loginArgs.putString("targetPath",path);
            Router.getARouter().build(Paths.LoginActivity).with(loginArgs).navigation();
            return;
        }
        callback.onContinue(postcard);
    }

    private boolean isLogin() {
        return App.isLogin();
    }

    /**
     * Do your init work in this method, it well be call when processor has been load.
     *
     * @param context ctx
     */
    @Override
    public void init(Context context) {

    }


}
